<template>
  <el-dialog v-model="visible" :title="!dataForm.fltId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="航班ID" prop="fltId">
        <el-input v-model="dataForm.fltId" placeholder="航班ID"></el-input>
      </el-form-item>
      <el-form-item label="航班日期" prop="fltDate">
        <el-input v-model="dataForm.fltDate" placeholder="航班日期"></el-input>
      </el-form-item>
      <el-form-item label="航班号" prop="fltNo">
        <el-input v-model="dataForm.fltNo" placeholder="航班号"></el-input>
      </el-form-item>
      <el-form-item label="飞机号" prop="acReg">
        <el-input v-model="dataForm.acReg" placeholder="飞机号"></el-input>
      </el-form-item>
      <el-form-item label="起飞机场四字码" prop="depApt4">
        <el-input v-model="dataForm.depApt4" placeholder="起飞机场四字码"></el-input>
      </el-form-item>
      <el-form-item label="到达机场四字码" prop="arrApt4">
        <el-input v-model="dataForm.arrApt4" placeholder="到达机场四字码"></el-input>
      </el-form-item>
      <el-form-item label="起飞机场三字码" prop="depApt3">
        <el-input v-model="dataForm.depApt3" placeholder="起飞机场三字码"></el-input>
      </el-form-item>
      <el-form-item label="到达机场三字码" prop="arrApt3">
        <el-input v-model="dataForm.arrApt3" placeholder="到达机场三字码"></el-input>
      </el-form-item>
      <el-form-item label="起飞时间" prop="depTime">
        <el-input v-model="dataForm.depTime" placeholder="起飞时间"></el-input>
      </el-form-item>
      <el-form-item label="到达时间" prop="arrTime">
        <el-input v-model="dataForm.arrTime" placeholder="到达时间"></el-input>
      </el-form-item>
      <el-form-item label="返航备降标识" prop="flgVr1">
        <el-input v-model="dataForm.flgVr1" placeholder="返航备降标识"></el-input>
      </el-form-item>
      <el-form-item label="航班性质" prop="fltType">
        <el-input v-model="dataForm.fltType" placeholder="航班性质"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="国内/国际/地区" prop="dOrI">
        <el-input v-model="dataForm.dOrI" placeholder="国内/国际/地区"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  fltId: "",
  fltDate: "",
  fltNo: "",
  acReg: "",
  depApt4: "",
  arrApt4: "",
  depApt3: "",
  arrApt3: "",
  depTime: "",
  arrTime: "",
  flgVr1: "",
  fltType: "",
  createTime: "",
  dOrI: "",
});

const rules = ref({
});

const init = (fltId?: number) => {
  visible.value = true;
  dataForm.fltId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (fltId) {
    getInfo(fltId);
  }
};

// 获取信息
const getInfo = (fltId: number) => {
  baseService.get("/sys/busfltinfo/" + fltId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.fltId ? baseService.post : baseService.put)("/sys/busfltinfo", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>